import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import viteCompression from "vite-plugin-compression";
import path from 'path'

export default defineConfig(({ command }) => {
  return {
    plugins: [vue(), createSvgIconsPlugin({
      // Specify the icon folder to be cached  
      iconDirs: [path.resolve(process.cwd(), 'src/assets/svg')],
      // Specify symbolId format  
      symbolId: 'icon-[dir]-[name]',
    }),AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),viteCompression(
      {
        algorithm: 'gzip',
        threshold: 10240,
        verbose: true,
        // deleteOriginFile: true
      }
    ),],
    base: './',
    resolve: {
      alias: {
        '@': '/src'
      },
      extensions: ['.js', '.ts', '.vue', '.json', '.css']
    },
    //需要自己新建一个存放共享的变量的文件如:src/styles/main.scss
    css: {
      preprocessorOptions: {
        scss: {
          javascriptEnabled: true,
          additionalData: '@import "./src/styles/main.scss";',
        },
      },
    },
    server: {
      port: 3000,
      open: true,
    },
    build: {
      rollupOptions: {
        output: {
          entryFileNames: `assets/[name].[hash].js`,
          chunkFileNames: `assets/[name].[hash].js`,
          assetFileNames: (assetInfo) => {
            // 这个条件判断检查 assetInfo.name 是否等于 'print-lock.css'。如果是，则返回 'css/[name].[ext]'，表示将该文件放在 css 目录下。
            if (assetInfo.name === 'print-lock.css') {
              return 'css/[name].[ext]';
            }
            return `assets/[name].[hash].[ext]`;
          }
        },
      },
    },
  }
})